home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
borland
/
tpwn31.zip
/
OLEDEMO.ZIP
/
OLEDEMO.PAS
next >
Wrap
Pascal/Delphi Source File
|
1992-04-06
|
34KB
|
1,170 lines
{**************************************************}
{ }
{ Turbo Pascal for Windows }
{ Object Linking and Embedding demo program }
{ }
{ Copyright (c) 1992 by Borland International }
{ }
{**************************************************}
program OleDemo;
{ This program demonstrates how to implement an OLE client application.
The program uses the new Ole, ShellAPI, and CommDlg units, and requires
that the OLECLI.DLL, SHELL.DLL, and COMMDLG.DLL libraries are present.
The program allows you to create embedded and linked objects using the
Edit|Paste and Edit|Paste link commands. The OLE objects can be moved
and resized, and they can be activated through double clicks or using
the Edit|Object menu. Workspaces can be saved and loaded using the
File menu. }
uses Strings, WinTypes, WinProcs, WObjects, Ole, ShellAPI, CommDlg;
{$R OLEDEMO}
const
{ Resource IDs }
id_Menu = 100;
id_About = 100;
{ Menu command IDs }
cm_FileNew = 100;
cm_FileOpen = 101;
cm_FileSave = 102;
cm_FileSaveAs = 103;
cm_FileExit = 104;
cm_EditCut = 200;
cm_EditCopy = 201;
cm_EditPaste = 202;
cm_EditPasteLink = 203;
cm_EditClear = 204;
cm_HelpAbout = 300;
cm_VerbMin = 900;
cm_VerbMax = 999;
{ Menu item positions }
pos_Edit = 1; { Position of Edit item on main menu }
pos_Object = 6; { Position of Object item on Edit menu }
type
{ Pointer types }
PAppClient = ^TAppClient;
PAppStream = ^TAppStream;
PObjectWindow = ^TObjectWindow;
PMainWindow = ^TMainWindow;
{ Filename string }
TFilename = array[0..255] of Char;
{ OLE file header }
TOleFileHeader = array[1..4] of Char;
{ Application client structure }
TAppClient = record
OleClient: TOleClient;
ObjectWindow: PObjectWindow;
end;
{ Application stream structure }
TAppStream = record
OleStream: TOleStream;
OwlStream: PStream;
end;
{ OLE object window }
TObjectWindow = object(TWindow)
AppClient: TAppClient;
OleObject: POleObject;
Framed: Boolean;
constructor Init(Link: Boolean);
constructor Load(var S: TStream);
destructor Done; virtual;
function GetClassName: PChar; virtual;
procedure GetWindowClass(var AWndClass: TWndClass); virtual;
procedure SetupWindow; virtual;
procedure Store(var S: TStream); virtual;
function CanClose: Boolean; virtual;
procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
procedure GetObjectClass(ClassName: PChar);
procedure Check(OleStatus: TOleStatus);
procedure OpenObject(Verb: Word);
procedure CloseObject;
procedure CopyToClipboard;
procedure Delete;
procedure Update;
procedure BringToFront;
procedure GetBounds(var R: TRect);
procedure SetBounds(var R: TRect);
procedure ShowFrame(EnableFrame: Boolean);
procedure WMGetMinMaxInfo(var Msg: TMessage);
virtual wm_First + wm_GetMinMaxInfo;
procedure WMMove(var Msg: TMessage);
virtual wm_First + wm_Move;
procedure WMSize(var Msg: TMessage);
virtual wm_First + wm_Size;
procedure WMLButtonDown(var Msg: TMessage);
virtual wm_First + wm_LButtonDown;
procedure WMMouseMove(var Msg: TMessage);
virtual wm_First + wm_MouseMove;
procedure WMLButtonUp(var Msg: TMessage);
virtual wm_First + wm_LButtonUp;
procedure WMLButtonDblClk(var Msg: TMessage);
virtual wm_First + wm_LButtonDblClk;
end;
{ Application main window }
TMainWindow = object(TWindow)
ObjectWindow: PObjectWindow;
ClientDoc: LHClientDoc;
Modified: Boolean;
Filename: TFilename;
constructor Init;
destructor Done; virtual;
function CanClose: Boolean; virtual;
procedure InitDocument;
procedure DoneDocument;
procedure SetFilename(Name: PChar);
function NewFile(Name: PChar): Boolean;
function LoadFile: Boolean;
function SaveFile: Boolean;
function Save: Boolean;
function SaveAs: Boolean;
procedure NewObjectWindow(Link: Boolean);
procedure SelectWindow(Window: PObjectWindow);
procedure UpdateObjectMenu;
procedure WMLButtonDown(var Msg: TMessage);
virtual wm_First + wm_LButtonDown;
procedure WMInitMenu(var Msg: TMessage);
virtual wm_First + wm_InitMenu;
procedure CMFileNew(var Msg: TMessage);
virtual cm_First + cm_FileNew;
procedure CMFileOpen(var Msg: TMessage);
virtual cm_First + cm_FileOpen;
procedure CMFileSave(var Msg: TMessage);
virtual cm_First + cm_FileSave;
procedure CMFileSaveAs(var Msg: TMessage);
virtual cm_First + cm_FileSaveAs;
procedure CMFileExit(var Msg: TMessage);
virtual cm_First + cm_FileExit;
procedure CMEditCut(var Msg: TMessage);
virtual cm_First + cm_EditCut;
procedure CMEditCopy(var Msg: TMessage);
virtual cm_First + cm_EditCopy;
procedure CMEditPaste(var Msg: TMessage);
virtual cm_First + cm_EditPaste;
procedure CMEditPasteLink(var Msg: TMessage);
virtual cm_First + cm_EditPasteLink;
procedure CMEditClear(var Msg: TMessage);
virtual cm_First + cm_EditClear;
procedure CMHelpAbout(var Msg: TMessage);
virtual cm_First + cm_HelpAbout;
procedure DefCommandProc(var Msg: TMessage); virtual;
end;
{ Application object }
TApp = object(TApplication)
constructor Init(AName: PChar);
destructor Done; virtual;
procedure InitMainWindow; virtual;
end;
{ Initialized globals }
const
Dragging: Boolean = False;
OleFileHeader: TOleFileHeader = 'TPOF';
OleProtocol: PChar = 'StdFileEditing';
OleObjectName: PChar = 'Object';
OleDemoTitle: PChar = 'OLE Demo';
{ Global variables }
var
App: TApp;
DragPoint: TPoint;
MainWindow: PMainWindow;
OleClientVTbl: TOleClientVTbl;
OleStreamVTbl: TOleStreamVTbl;
PixPerInch: TPoint;
CFObjectLink, CFOwnerLink: Word;
{ TObjectWindow stream registration record }
const
RObjectWindow: TStreamRec = (
ObjType: 999;
VmtLink: Ofs(TypeOf(TObjectWindow)^);
Load: @TObjectWindow.Load;
Store: @TObjectWindow.Store);
{ Display an error message using the MessageBox API routine. }
procedure Error(Message, Argument: PChar);
var
S: array[0..255] of Char;
begin
wvsprintf(S, Message, Argument);
MessageBox(0, S, OleDemoTitle, mb_IconExclamation + mb_Ok);
end;
{ Display OLE operation error message. }
procedure OleError(Status: Word);
var
S: array[0..7] of Char;
begin
wvsprintf(S, '%d', Status);
Error('Warning: OLE operation failed, error code = %s.', S);
end;
{ Display an Open or Save As file dialog using the Common Dialog DLL. }
function FileDialog(Owner: HWnd; Filename: PChar; Save: Boolean): Boolean;
const
DefOpenFilename: TOpenFilename = (
lStructSize: SizeOf(TOpenFilename);
hwndOwner: 0;
hInstance: 0;
lpstrFilter: 'OLE files (*.OLE)'#0'*.ole'#0;
lpstrCustomFilter: nil;
nMaxCustFilter: 0;
nFilterIndex: 0;
lpstrFile: nil;
nMaxFile: SizeOf(TFilename);
lpstrFileTitle: nil;
nMaxFileTitle: 0;
lpstrInitialDir: nil;
lpstrTitle: nil;
Flags: 0;
nFileOffset: 0;
nFileExtension: 0;
lpstrDefExt: 'ole');
var
OpenFilename: TOpenFilename;
begin
OpenFilename := DefOpenFilename;
OpenFilename.hwndOwner := Owner;
OpenFilename.lpstrFile := Filename;
if Save then
begin
OpenFilename.Flags := ofn_PathMustExist + ofn_NoChangeDir +
ofn_OverwritePrompt;
FileDialog := GetSaveFilename(OpenFilename);
end else
begin
OpenFileName.Flags := ofn_PathMustExist;
FileDialog := GetOpenFilename(OpenFilename);
end;
end;
{ OLE client callback routine. Called by the OLE client library to notify
the application of any changes to an object. I